//-----------------------------------------------------------------------
// <copyright file="TimeLogPresenter.cs" company="Simnova LLC">
//     Copyright (c) Simnova LLC. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Simnova.Collaborate.Presenter
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Simnova.Collaborate.View;

    public class TimeLogPresenter
    {
        private ITimeLog timeLogView;
        private int userId;
        private AppServer.TimeLog timeLog;

        public TimeLogPresenter(ITimeLog timeLogView, int userId)
        {
            this.timeLogView = timeLogView;
            this.userId = userId;
            this.LoadLogEntries();
            this.LoadCompanies();
            this.LoadRoles();
            this.LoadActivityTypes();
            this.LoadTasks();
            AppServer.CollaborateServiceClient collaborateClient = new Simnova.Collaborate.Presenter.AppServer.CollaborateServiceClient();
            this.timeLog = collaborateClient.GetCurrentTimeLog(this.userId);
            timeLogView.StartTime = this.timeLog.StartTime;
        }

        public void LoadRoles()
        {
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            this.timeLogView.Roles = collaborateClient.GetRolesByUserCompany(this.userId, this.timeLogView.SelectedCompanyId);
            collaborateClient.Close();
        }

        public void LoadActivityTypes()
        {
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            this.timeLogView.ActivityTypes = collaborateClient.GetActivityTypesByRole(this.timeLogView.SelectedRoleId);
            collaborateClient.Close();
        }

        public void LoadTasks()
        {
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            this.timeLogView.Tasks = collaborateClient.GetTasksByUser(this.userId);
            collaborateClient.Close();
        }
        
        public void DeleteTimeLogEntry()
        {
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            collaborateClient.DeleteTimeLog(this.timeLog);
            collaborateClient.Close();
            this.timeLog.RemainingHours = 0;
        }

        public void GetCurrentTimeLogEntry()
        {
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            this.timeLog = collaborateClient.GetCurrentTimeLog(this.userId);
            collaborateClient.Close();
            this.timeLogView.StartTime = this.timeLog.StartTime;
            this.LoadLogEntries();
        }

        public void SaveTimeLogEntry()
        {
            this.timeLog.Description = this.timeLogView.Description;
            this.timeLog.RemainingHours = this.timeLogView.RemainingHours;
            this.timeLog.ActivityTypeId = this.timeLogView.SelectedActivityTypeId;
            this.timeLog.TaskId = this.timeLogView.SelectedTaskId;
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            collaborateClient.SaveTimeLog(this.timeLog);
            collaborateClient.Close();
            this.timeLog.RemainingHours = 0;
        }

        private void LoadCompanies()
        {
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            this.timeLogView.Companies = collaborateClient.GetCompaniesByUser(this.userId);
            collaborateClient.Close();
        }

        private void LoadLogEntries()
        {
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            this.timeLogView.LogEntries = collaborateClient.GetTimeLogByUser(this.userId);
            collaborateClient.Close();
        }
    }
}
